package Sort;


import java.util.Arrays;
import java.util.Random;

import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;

public class BinarySearchTestTheories {

	@Before
	public void setUp() throws Exception {
		
		
	}
	
	//
	public int[] generateRandomSortedArray(int maxArraySize, int maxValue) { 
		
			      int arraySize = 1 + rand.nextInt(maxArraySize); 
			      int[] randomArray = new int[arraySize]; 
			      for (int i = 0; i < arraySize; i++) { 
			          randomArray[i] = rand.nextInt(maxValue); 
			      } 
			      Arrays.sort(randomArray); 
			      return randomArray; 
			 }
	
	 Random rand; 
	 
	 @Before 
	  public void initialize() { 
	      rand = new Random(); 
	 } 
	 
	 @Test 
	  public void testTheories() { 
	 
	      int maxArraySize = 1000; 
	      int maxValue = 1000; 
	      int experiments = 1000; 
	      int[] testArray; 
	      int target; 
	      int returnValue; 
	 
	      while (experiments-- > 0) { 
	          testArray = generateRandomSortedArray(maxArraySize, 	maxValue); 
	          if (rand.nextBoolean()) { 
	              target = 	testArray[rand.nextInt(testArray.length)]; 
	          } else { 
	              target = rand.nextInt(); 
	          } 
	          returnValue = BinarySearch.buggyBinarySearch(testArray, target); 
	          assertTheory1(testArray, target, returnValue); 
	          assertTheory2(testArray, target, returnValue); 
	      } 
	 } 
	 
	  public void assertTheory1(int[] testArray, int target, int returnValue) { 
	      if (returnValue == -1) 
	    	  Assert.assertFalse(arrayContainsTarget(testArray, target)); 
	 } 
	 
	  public void assertTheory2(int[] testArray, int target, int 	returnValue) {
		  
	      if (returnValue >= 0) 	    	  
	          Assert.assertEquals(target, testArray[returnValue]); 	      
	     
	 } 
	 
	  public boolean arrayContainsTarget(int[] testArray, int 	target) { 
		  
	      for (int i = 0; i < testArray.length; i++) 
	          if (testArray[i] == target) 
	              return true; 
	      return false; 
	 }
	

}
